<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>geo_centroid 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-geocentroid-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-geocentroid-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-geocentroid-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-geocentroid-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-geocentroid-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">geo_centroid 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-geobounds-aggregation.html">« geo_bounds 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-max-aggregation.html">max 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-geocentroid-aggregation"></a>geo_centroid 聚合
</h2>
</div></div></div>
<p>
根据 <a class="xref" href="geo-point.html" title="geo-point 数据类型">geo-point</a> 字段的所有坐标值计算加权 <a href="https://en.wikipedia.org/wiki/Centroid" class="ulink" target="_top">矩心(centroid)</a> 的度量聚合。
</p>
<p>示例：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /museums
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_point"
            }
        }
    }
}

POST /museums/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "city": "Amsterdam", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "city": "Antwerp", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "city": "Paris", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "city": "Paris", "name": "Musée d'Orsay"}

POST /museums/_search?size=0
{
    "aggs" : {
        "centroid" : {
            "geo_centroid" : {
                "field" : "location" <a id="CO190-1"></a><i class="conum" data-value="1"></i>
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/351.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO190-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
<code class="literal">geo_centroid</code> 聚合指定用于计算矩心的字段。（注意：字段必须是<a class="xref" href="geo-point.html" title="geo-point 数据类型">geo-point</a>类型）
</p>
</td>
</tr>
</table>
</div>
<p>
上面的聚合演示了如何计算博物馆(museum)的所有文档的 location(位置) 字段的矩心。
</p>
<p>上面聚合的响应为：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "centroid": {
            "location": {
                "lat": 51.00982965203002,
                "lon": 3.9662131341174245
            },
            "count": 6
        }
    }
}</pre>
</div>
<p>
当 <code class="literal">geo_centroid</code> 聚合作为其他桶聚合的子聚合时，会更有意思。
</p>
<p>例如：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /museums/_search?size=0
{
    "aggs" : {
        "cities" : {
            "terms" : { "field" : "city.keyword" },
            "aggs" : {
                "centroid" : {
                    "geo_centroid" : { "field" : "location" }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/352.console"></div>
<p>
上面的示例使用 <code class="literal">geo_centroid</code> 作为 <a class="xref" href="search-aggregations-bucket-terms-aggregation.html" title="terms 聚合" rel="nofollow">terms</a> 桶聚合的子聚合，用于查找每个城市中博物馆的中心位置。
</p>
<p>上面聚合的响应为：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "cities": {
            "sum_other_doc_count": 0,
            "doc_count_error_upper_bound": 0,
            "buckets": [
               {
                   "key": "Amsterdam",
                   "doc_count": 3,
                   "centroid": {
                      "location": {
                         "lat": 52.371655656024814,
                         "lon": 4.909563297405839
                      },
                      "count": 3
                   }
               },
               {
                   "key": "Paris",
                   "doc_count": 2,
                   "centroid": {
                      "location": {
                         "lat": 48.86055548675358,
                         "lon": 2.3316944623366
                      },
                      "count": 2
                   }
                },
                {
                    "key": "Antwerp",
                    "doc_count": 1,
                    "centroid": {
                       "location": {
                          "lat": 51.22289997059852,
                          "lon": 4.40519998781383
                       },
                       "count": 1
                    }
                 }
            ]
        }
    }
}</pre>
</div>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<h3>使用<code class="literal">geo_centroid</code>作为<code class="literal">geohash_grid</code>的子聚合
</h3>
<p>
<a class="xref" href="search-aggregations-bucket-geohashgrid-aggregation.html" title="geohash grid 聚合" rel="nofollow"><code class="literal">geohash_grid</code></a> 聚合将文档而不是单个 geo-point 放入桶中。

如果文档的 <code class="literal">geo_point</code>字段包含<a class="xref" href="array.html" title="Arrays">多值(multiple values)</a>，则该文档可以被分配给多个桶，即使一个或多个 geo-point 在桶边界之外。
</p>
<p>
如果还用了 <code class="literal">geocentroid</code> 子聚合，则使用桶中的所有 geo-point (包括桶边界之外的 geo-point )来计算每个矩心。这可能会导致矩心位于桶边界之外。
</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-geobounds-aggregation.html">« geo_bounds 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-max-aggregation.html">max 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>